package com.bite.common.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.bite.common.core.contants.Constants;
import com.bite.common.core.utils.ThreadLocalUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "createBy", Long.class, 1L);//获取当前用户id,先放着
        this.strictInsertFill(metaObject, "createBy", Long.class, ThreadLocalUtil.get(Constants.USER_ID,Long.class));//获取当前用户id,先放着

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //System.out.println("metaObject:"+metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.strictUpdateFill(metaObject,"updateBy",Long.class,  ThreadLocalUtil.get(Constants.USER_ID,Long.class));
    }
}
